package com.example.netty.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.DefaultByteBufHolder;
import io.netty.buffer.Unpooled;
import io.netty.util.ByteProcessor;
import io.netty.util.CharsetUtil;
import sun.security.util.ByteArrayLexOrder;

import java.nio.ByteOrder;

public class fubExample {
    public static void main(String[] args) {
        ByteBuf buffer = Unpooled.buffer(10);
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.writeByte(i);
        }
        for (int i = 0; i < buffer.capacity(); i++) {
            System.out.print(buffer.readByte()+" ");
        }
        System.out.println();

        ByteBuf buf = Unpooled.copiedBuffer("hello，北京", CharsetUtil.UTF_8);
        byte[] array = buf.array();
        buf.writeByte('w');
        System.out.println((char) buf.readByte());
        System.out.println(buf.getCharSequence(1, 3, CharsetUtil.UTF_8));
        buf.writeBytes(new byte[]{'a','d','s'});
        System.out.println(new String(array,CharsetUtil.UTF_8).trim());

        //查找字符
        int index = buf.forEachByte(new ByteProcessor.IndexOfProcessor((byte) 'c'));
        int index2 = buf.forEachByte(ByteProcessor.FIND_CR);    //回车符
        //派生缓冲区
        ByteBuf slice = buf.slice();
        ByteBuf slice2 = buf.slice(2,3);
        ByteBuf slice3 = buf.readSlice(3);
        ByteBuf duplicate = buf.duplicate();
        ByteBuf buf1 = Unpooled.unmodifiableBuffer();
        ByteBuf order = buf.order(ByteOrder.nativeOrder()); //?
        //真的复制要用copy（）
        ByteBuf copy = buf.copy();
        //支撑数组
        byte[] array1 = buf.array();
        //ByteBuf的容器,对byteBuf的管理封装了一些方法
        ByteBufHolder byteBufHolder = new DefaultByteBufHolder(buf);



    }
}
